//
//  SHHGetData.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/11.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

typealias SHHMyBlock = (_ :Array<Array<WordTravelModel>>)->Void
typealias CityBlock = (_ :Array<CityModel>)->Void
typealias CityDetailBlock = (_ :Array<CityDetailModel>)->Void

class SHHGetData: NSObject {
    
    //存放世界的数组
    var arrayWithArray = Array<Array<WordTravelModel>>()
    //存放国家的数组
    var cityArray = Array<CityModel>()
    //存放国家详情的数组
    var cityDetailArray = Array<CityDetailModel>()
    
    private override init() { }
    static var sharedGetData = SHHGetData()
    
    func requestAllModelWithUrlString(urlString:String,success:SHHMyBlock)->Void {
        
        let url = NSURL(string: urlString)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            
            do{
                let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let array = dic["continents"] as! NSArray
                for dict in array {
                    let arr = dict["countrys"] as! NSArray
                    var dataArray = Array<WordTravelModel>()
                    for Dic in arr {
                        let model = WordTravelModel()
                        model.setValuesForKeysWithDictionary(Dic as! [String : AnyObject])
                        dataArray.append(model)
                    }
                    self.arrayWithArray.append(dataArray)
                }
                
            }catch{
                
            }
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                success(self.arrayWithArray)
            })
            
        })
        task.resume()
        
    }
    
    func requestCityModelWithUrlString(urlString:String,success:CityBlock)->Void {
        
        let url = NSURL(string: urlString)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            
            do{
                let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let array = dic["places"] as! NSArray
                for dict in array {
                    let model = CityModel()
                    model.setValuesForKeysWithDictionary(dict as! [String : AnyObject])
                    self.cityArray.append(model)
                }
                
            }catch{
                
            }
            
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                success(self.cityArray)
            })
            
        })
        task.resume()
        
    }
    
    
    //解析城市的详情的model
    func requestCityDetailModelWithUrlString(urlString:String,success:CityDetailBlock)->Void {
//        self.cityDetailArray.removeAll()
        let url = NSURL(string: urlString)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            
            do{
                let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let array = dic["list"] as! NSArray
                for dict in array {
                    let model = CityDetailModel()
                    model.setValuesForKeysWithDictionary(dict as! [String : AnyObject])
                    self.cityDetailArray.append(model)
                }
                
            }catch{
                
            }
            
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                success(self.cityDetailArray)
            })
            
        })
        task.resume()
        
    }
    
}
